/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.img;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: app2022 - QrCode1
 * <p>Powered by webrx On 2022-01-24 09:32:45
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class QrCode1 {
    public static void main(String[] args) throws Exception {
        //读取QRcode
        var img = ImageIO.read(new File("qr.png"));
        var src = new BufferedImageLuminanceSource(img);
        var binarizer = new HybridBinarizer(src);
        var binaryBitmap = new BinaryBitmap(binarizer);
        var result = new QRCodeReader().decode(binaryBitmap);
        System.out.println(result.getText());
    }

    public static void write(String[] args) throws Exception {
        //写入QrCode1
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        var bm = new QRCodeWriter().encode("北游教育130", BarcodeFormat.QR_CODE, 200, 200, hints);
        var i = MatrixToImageWriter.toBufferedImage(bm);
        ImageIO.write(i, "png", new FileOutputStream("qr.png"));
        Runtime.getRuntime().exec("cmd /k start qr.png");
    }
}
